package com.zahb.qadx.net.interceptor;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zahb.qadx.MainActivity;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.ui.activity.LoginActivity;
import com.zahb.qadx.util.ActivityManager;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by Zdw on 2020/10/30 13:51
 */
public class Code203Interceptor implements Interceptor {

    @NotNull
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());
        Response.Builder builder = response.newBuilder();
        Response clone = builder.build();
        ResponseBody body = clone.body();
        if (body != null) {
            MediaType mediaType = body.contentType();
            if (mediaType != null) {
                if (mediaType.subtype().equals("json") || mediaType.type().equals("text")) {
                    String resp = body.string();
                    if (resp.startsWith("{") && resp.endsWith("}")) {
                        JsonObject object = JsonParser.parseString(resp).getAsJsonObject();
                        if (object.has("statusCode")) {
                            int statusCode = object.get("statusCode").getAsInt();
                            if ((statusCode == 203 || statusCode == 207) && MainActivity.sMainActivity != null) {
                                if (object.has("errorInfo")) {
                                    JsonElement element = object.get("errorInfo");
                                    if (!(element instanceof JsonNull)) {
                                        MainActivity.sMainActivity.showToast(element.getAsString());
                                        BaseApplication.getContext().setDataLogin(null);
                                        SharedPreferences sp = BaseApplication.getContext()
                                                .getSharedPreferences("AccountInfo", Context.MODE_PRIVATE);
                                        sp.edit().putString("loginData", "").apply();
                                        String[] array = LoginActivity.getAccountInfo(MainActivity.sMainActivity);
                                        ActivityManager.getInstance().finishActivities(ActivityManager.KEY_ALL_ACTIVITY);
                                        if (LoginActivity.sLoginActivity == null)
                                            LoginActivity.actionStart(MainActivity.sMainActivity, array[0], array[1]);
                                    }
                                }
                            }
                        }
                    }
                    body = ResponseBody.create(mediaType, resp);
                    return response.newBuilder().body(body).build();
                }
            }
        }
        return response;
    }
}
